package com.csxy.six.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.six.R2;
import com.csxy.six.entity.Blacklist;
import com.csxy.six.entity.User;
import com.csxy.six.exception.MyException;
import com.csxy.six.mapper.BlacklistMapper;
import com.csxy.six.mapper.UserMapper;
import com.csxy.six.service.BlacklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements BlacklistService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public R2<List<Blacklist>> getAllBlacklist() {
        return R2.ok(this.list());
    }

    @Override
    @Transactional
    public R2<Void> addToBlacklist(Long userId, String reason, LocalDateTime endTime) {
        // 检查用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new MyException(404, "用户不存在");
        }

        // 检查用户是否已在黑名单
        QueryWrapper<Blacklist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (this.count(wrapper) > 0) {
            throw new MyException(400, "用户已在黑名单中");
        }

        // 添加到黑名单
        Blacklist blacklist = new Blacklist();
        blacklist.setUserId(userId);
        blacklist.setReason(reason);
        blacklist.setStartTime(LocalDateTime.now());
        blacklist.setEndTime(endTime);
        this.save(blacklist);

        // 更新用户状态
        user.setStatus(0); // 禁用状态
        userMapper.updateById(user);

        return R2.ok().message("用户已加入黑名单");
    }

    @Override
    @Transactional
    public R2<Void> removeFromBlacklist(Long userId) {
        // 从黑名单移除
        QueryWrapper<Blacklist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        this.remove(wrapper);

        // 更新用户状态
        User user = userMapper.selectById(userId);
        if (user != null) {
            user.setStatus(1); // 正常状态
            userMapper.updateById(user);
        }

        return R2.ok().message("用户已移出黑名单");
    }

    @Override
    public R2<Boolean> checkIfBanned(Long userId) {
        QueryWrapper<Blacklist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .and(qw -> qw.gt("end_time", LocalDateTime.now()).or().isNull("end_time"));
        return R2.ok(this.count(wrapper) > 0);
    }

    @Override
    public R2<Blacklist> getBlacklistById(Long id) {
        Blacklist blacklist = this.getById(id);
        if (blacklist == null) {
            throw new MyException(404, "黑名单记录不存在");
        }
        return R2.ok(blacklist);
    }

    @Override
    @Transactional
    public R2<Blacklist> updateBlacklist(Long id, String reason, LocalDateTime endTime) {
        Blacklist blacklist = this.getById(id);
        if (blacklist == null) {
            throw new MyException(404, "黑名单记录不存在");
        }

        if (reason != null) {
            blacklist.setReason(reason);
        }

        if (endTime != null) {
            blacklist.setEndTime(endTime);
        }

        this.updateById(blacklist);
        return R2.ok(blacklist).message("黑名单记录更新成功");
    }

    @Override
    @Transactional
    public R2<Void> removeFromBlacklistById(Long id) {
        if (!this.removeById(id)) {
            throw new MyException(404, "黑名单记录不存在");
        }
        return R2.ok().message("黑名单记录删除成功");
    }
}